const formatDate = (t1, format = 'YYYY-MM-DD HH:mm:ss') => {
    const config = {
        YYYY: t1.getFullYear(),
        MM: (t1.getMonth() + 1).toString().padStart(2, '0'),
        DD: t1.getDate().toString().padStart(2, '0'),
        HH: t1.getHours().toString().padStart(2, '0'),
        mm: t1.getMinutes().toString().padStart(2, '0'),
        ss: t1.getSeconds().toString().toString().padStart(2, '0')
    };
    for (const key in config) {
        format = format.replace(key, config[key])
    }
    return format
}
const parseObjectUrl = (url) => {
    let arr = url.split('?');
    let brr = arr[1].split('&');
    let obj = {};
    for (let i = 0; i < brr.length; i++) {
        let crr = brr[i].split('=');
        obj[crr[0]] = crr[1];
    }
    return obj;
}
// -------------------------- 工具函数工具函数 --------------------------
/**
 * 统一响应处理函数
 * @param {Object} ctx - Koa上下文
 * @param {number} code - 状态码
 * @param {string} msg - 消息
 * @param {any} data - 数据
 */
const sendResponse = (ctx, code, msg, data = [], obj = {}) => {
    ctx.status = code || 500;
    let res = { code, msg, data };
    Object.assign(res, obj);
    ctx.body = res;
};

//开发环境dev 部署环境 prod
const environment = process.env.NODE_ENV || 'prod';
//部署时更换 '8.137.37.23'
const ip = environment === 'dev' ? 'localhost' : '8.137.37.23'
// -------------------------- 常量定义 --------------------------
const PORT = 6789; // 提取端口常量，便于维护
const JWT_SECRET = "yh_work"; // JWT密钥
const default_avatar = `http://${ip}:${PORT}/defaultImgs/defaultAvatar.png`;

module.exports = {
    ip,
    environment,
    PORT,
    JWT_SECRET,
    default_avatar,
    formatDate,
    parseObjectUrl,
    sendResponse
}